#ifndef __THREAD_POOL_2_H__
#define __THREAD_POOL_2_H__

#include "OSType.h"
#include "Mutex.h"
#include "Thread.h"
#include <string>
#include <deque>
#include <vector>
#include "Condition.h"

using namespace std;

typedef void(*TaskFunc)(void*);

typedef struct Task
{
	TaskFunc pFunc;
	void*    pArg;
}Task;

class CThreadPool2
{
public:
	CThreadPool2();
	~CThreadPool2();

	void SetMaxQueueSize(int maxSize) { m_nMaxQueueSize = maxSize; }

	void Start(Int32 nNumThreads, Int32 nCoreIdx);
	void Stop();

	void Run(TaskFunc pFunc, void *arg);

	UInt32 GetQueueSize() const;
	void  RunInThread();
	

private:
	Bool IsFull() const;
	Task* Take();


	mutable CMutex m_mutex;
	CCondition m_condNotEmpty;
	CCondition m_condNotFull;

	deque<Task*>	m_queueTask;
	UInt32			m_nMaxQueueSize;
	Bool			m_bRunning;

	vector<CThread*> m_vecThreads;
};

class CWorkerThread : public CThread
{
public:
	CWorkerThread(Int32 nCPUIdx, CThreadPool2* pThreadPool);
	virtual ~CWorkerThread();

protected:
	virtual Bool OnCreateThread(UInt32 &dwStackSize, Int32 &nCPUIndex);
	virtual void OnRun();

protected:
	Int32	m_nCPUIndex;
	CThreadPool2* m_pThreadPool;
};

#endif

